import pytest

import config
from API.contract import ConteractAPI
from API.login import LoginAPI
from API.couese import CourseAPI
from common.read_json_data import read_json
from common.assertutil import assert_until
from common.dbutil import DBUtil


class TestLogin:
    #
    fileName = None
    token = None
    id = None

    # 类前置处理
    def setup_class(self):
        # 清理测试数据：删除指定编号的合同信息
        DBUtil().exe_sql("delete from tb_contract where contract_no like 'HT1101211%';")

    def setup(self):
        # 实例化对象
        self.loginapi = LoginAPI()
        self.courseapi = CourseAPI()
        self.conteractapi = ConteractAPI()

    #
    def teardown(self):
        pass

    @pytest.mark.parametrize("expect,username,password,status,message,code", read_json("/data/login_data.json"))
    def test01_login(self, expect, username, password, status, message, code):
        response = self.loginapi.get_Verification_code()
        uuid = response.json().get("uuid")
        login_data = {
            "username": username,
            "password": password,
            "code": 2,
            "uuid": uuid
        }
        response = self.loginapi.login(login_data)
        print(response.json())
        assert_until(response, status, message, code)
        print(f"expect={expect}", response.json())
        TestLogin.token = response.json().get("token")
        print("-"*30)

    def test02_insert_course(self):
        insert_course_json = {
            "name": "测试开发提升课01",
            "subject": "6",
            "price": 899,
            "applicablePerson": "2",
            "info": "测试开发提升课01"
        }
        response = self.courseapi.insert_course(token=TestLogin.token, insert_course_json=insert_course_json)
        print(response.json())
        assert_until(response)

    def test03_select_course(self):
        response = self.courseapi.select_course(token=TestLogin.token)
        TestLogin.id = response.json().get("rows")[0].get("id")
        assert_until(response)
        print("-" * 30)

    def test04_update_course(self):
        update_course_json={"id": TestLogin.id,
                             "name": "接口测试001",
                             "subject": "6",
                             "price": 998,
                             "applicablePerson": "2",
                             "info": "课程介绍001"
                             }
        response = self.courseapi.upate_course(token=TestLogin.token,update_course_json=update_course_json)
        print(response.json())
        assert_until(response)
    #上传合同
    def test05_upload_conteract(self):
        file_data = open(config.BASE_DIR + "/data/test.pdf", "rb")
        response = self.conteractapi.upload_conteract(file_data=file_data,token=TestLogin.token)
        assert_until(response)
        TestLogin.fileName= response.json().get("fileName")
        print(TestLogin.fileName)
    #新增合同
    def test06_insert_conteract(self):
        insert_conteract_data={
            "name": "测试",
            "phone": "13612345123",
            "contractNo": "HT11012111253",
            "subject": "6",
            "courseId": 99,
            "fileName": TestLogin.fileName
    }
        response = self.conteractapi.insert_conteract(token=TestLogin.token,insert_conteract_data=insert_conteract_data)
        print(response.json())
        assert_until(response)
    #查询合同
    def test07_select_conteract(self):
        response = self.conteractapi.select_conteract(token=TestLogin.token,phone="13612345123")
        print(response.json().get("rows")[0].get("id"))
        TestLogin.id = response.json().get("rows")[0].get("id")

    def test08_delect_conteract(self):
        response = self.conteractapi.delect_conteract(token=TestLogin.token,id = TestLogin.id)
